package com.shujia.java.day19.demo6;
/**
* @Description: MyRunnable 网络购票100张票
* @Author: 王火火
* @Date: 2019/8/22 10:53
 *
 * 同步代码块
*/
public class MyRunnable implements Runnable{
    int sum=100;
    @Override
    public void run() {
        while (true){
            synchronized (this){
                if(sum>0){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"买了第"+(sum--)+"张票");
                }else {
                    break;
                }
            }
        }
    }
}
